<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <h2>watch做数据请求</h2>
    <input type="text" v-model="value">
    <ul>
      <li v-for="(item, index) in list" :key="index">{{ item.value }}</li>
    </ul>


    <hr>

    <ul>
      <li v-for="item in titleList" :key="item.id">{{ item.title }}</li>
    </ul>
    <span v-for="item in 4" style="margin: 0 10px;" @click="pageClick(item)">{{item}}</span>
  </div>

  <script src="../Vue.js"></script>

  <script>
    const { createApp } = Vue;

    createApp({
      data() {
        return {
          value: "",
          list: [],
          pageNum: 1,
          titleList: []
        }
      },
      // watch监听里面是可以写异步的
      watch: {
        value(val) {
          fetch(`http://127.0.0.1:3000/api/index/list2?key=${val}`)
            .then(response => response.json())
            .then(res => {
              console.log(res);
              this.list = res.data;
            })
        },
        // pageNum(val) {
        //   fetch(`http://127.0.0.1:3000/api/index/list3?pageNum=${val}`)
        //     .then(response => response.json())
        //     .then(res => {
        //       console.log(res);
        //       this.titleList = res.data
        //     })
        // }
        pageNum: {
          handler(val) {
            fetch(`http://127.0.0.1:3000/api/index/list3?pageNum=${val}`)
              .then(response => response.json())
              .then(res => {
                console.log(res);
                this.titleList = res.data
              })
          },
          immediate: true
        }
      },
      computed: {},
      methods: {
        pageClick(num) {
          this.pageNum = num;
        }
      },
    }).mount("#app")

  </script>
</body>
</html>